package main

import "fmt"

/**
 * @param nums: A list of integers.
 * @param k: An integer
 * @return: The maximum number inside the window at each moving.
 */
func MaxSlidingWindow(nums []int, k int) []int {
	// 滑动窗口
	ans := []int{}
	ll := []int{} //模拟双端队列，本答案中主要是对0位置和最后一个位置进行操作
	for i := 0; i < len(nums); i++ {
		size := len(ll)
		for size > 0 && nums[ll[size-1]] < nums[i] {
			ll = ll[:size-1]
			size = len(ll)
		}

		ll = append(ll, i)

		size = len(ll)
		if ll[0] == i-k {
			ll = ll[1:size]
		}

		size = len(ll)
		if ll[size-1] >= k-1 {
			ans = append(ans, nums[ll[0]])
		}

	}
	return ans
}

func main() {
	//arr := []int{1, 2, 7, 7, 8}
	//fmt.Println(MaxSlidingWindow(arr, 3))
	fmt.Println(MaxSlidingWindow([]int{1, 2, 7, 7, 2}, 3))
}

/*
[1,2,7,7,2]
3
输出数据
[7,7,2]
期望答案
[7,7,7]
*/
